﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using carXapp2;
using System.Collections.ObjectModel;
using System.ComponentModel;
using carXapp2.classes;

namespace carXapp2.FuncPages
{
    public partial class MaintInfo : PhoneApplicationPage
    {
        private int carID;
        private int maintID;
        private CarStats stats;

        public MaintInfo()
        {
            InitializeComponent();
            this.DataContext = App.ViewModel;
        }

        private void LoadData(int carID)
        {
            var maintInfoDb = from maintInfo data in App.ViewModel.carDataDb.maintInfo
                              where data.CarID == carID
                              orderby data.Date descending
                              select data;
            App.ViewModel.Maint = new ObservableCollection<maintInfo>(maintInfoDb);
            App.ViewModel.SaveChangesToDB();        //fix to save data on immediate exit

            var maintInfoforStats = from maintInfo info in App.ViewModel.carDataDb.maintInfo
                                   where info.CarID == carID
                                   orderby info.Date ascending
                                   select info;
            var CurrentMInfo = new ObservableCollection<maintInfo>(maintInfoforStats);
            stats = new CarStats(CurrentMInfo);
            CreateChart(0);
            chart1.DataContext = this;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string cid, mid;
            NavigationContext.QueryString.TryGetValue("carID", out cid);
            NavigationContext.QueryString.TryGetValue("maintID", out mid);
            int.TryParse(cid,out carID);
            int.TryParse(cid, out maintID);
            LoadData(carID);
            listbox1.SelectedIndex = -1;
            base.OnNavigatedTo(e);
        }

        private void menuAdd_click(object sender, EventArgs e)
        {
            //maintInfo newRec = new maintInfo();
            //newRec.Date = DateTime.Now;
            
            App.ViewModel.SingleMaintRecord = new maintInfo();
            App.ViewModel.SingleMaintRecord.CarID = carID;
            App.ViewModel.SingleMaintRecord.Date = DateTime.Now;
            NavigationService.Navigate(new Uri("/MiscPages/AddEditMaint.xaml?carID="+carID.ToString(), UriKind.Relative));
        }

        private void menuEdit_click(object sender, EventArgs e)
        {
            if (listbox1.SelectedIndex != -1)
            {
                var selectedRecord = listbox1.SelectedItem as maintInfo;
                var maintId = selectedRecord.MaintID;
                App.ViewModel.SingleMaintRecord = selectedRecord;
                NavigationService.Navigate(new Uri("/MiscPages/AddEditMaint.xaml?carID=" + carID.ToString() + "&maintID=" + maintID.ToString(), UriKind.Relative));
            }
            else
            {
                MessageBox.Show("please select a record");
            }
            
        }

        private void menuDelete_click(object sender, EventArgs e)
        {
            if (listbox1.SelectedIndex != -1)
            {
                var selectedRecord = listbox1.SelectedItem as maintInfo;
                //App.ViewModel.SingleMaintRecord = selectedRecord;
                App.ViewModel.DeleteMaint(selectedRecord);
            }
            else
            {
                MessageBox.Show("please select a record");
            }
            
        }
        private void CreateChart(int selection)
        {
            ChartDataSet.Clear();
            string[] monthList = new string[13] { "0", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            int count = 0;
            switch (selection)
            {
                case 0:
                    float[] dataList;

                    dataList = stats.GetMaintCostByMonth();

                    foreach (float item in dataList)
                    {
                        ChartData data = new ChartData() { val1 = monthList[count], val2 = item };
                        ChartDataSet.Add(data);
                        count++;
                    }
                    //graph1.Title = "Monthly cost on maint";
                    break;
                case 1:
                    dataList = null;
                    dataList = stats.GetMaintLaborCostByMonth();
                    foreach (float item in dataList)
                    {
                        ChartData data = new ChartData() { val1 = monthList[count], val2 = item };
                        ChartDataSet.Add(data);
                        count++;
                    }
                    //graph1.Title = "Monthly cost on maint";
                    break;
                case 2:
                     dataList = null;
                     dataList = stats.GetMaintPartCostByMonth();
                    foreach (float item in dataList)
                    {
                        ChartData data = new ChartData() { val1 = monthList[count], val2 = item };
                        ChartDataSet.Add(data);
                        count++;
                    }
                    //graph1.Title = "Monthly labor cost";
                    break;
            }

        }

        private void ChartSelectionChanged(object sender, MouseEventArgs e)
        {
            if (listpicker1.SelectedIndex != -1)
            {
                CreateChart(listpicker1.SelectedIndex);
            }
        }

        private ObservableCollection<ChartData> _ChartData = new ObservableCollection<ChartData>();

        public ObservableCollection<ChartData> ChartDataSet
        {
            get { return _ChartData; }
            set
            {
                _ChartData = value;
            }
        }
        public class ChartData
        {
            public string val1 { get; set; }
            public double val2 { get; set; }

        }
       
    }
}